var util = require('../../utils/util.js')
var app = getApp()
let videoAd = null
let zhi = app.globalData.jinbizhi.look_ads
let mubiao =
  Page({
    data: {
      lookVideosNum: 10,
      isDisabled:false
    },
    // 异步获取全局金币值
    getGlobalJInbizhi() {
      if (zhi == 0) {
        app.getGlobalJInbizhi().then(res => {
          zhi = res.look_ads
        })
      }
    },
    onLoad: function (options) {
      mubiao = app.globalData.userInfo._openid
      this.getGlobalJInbizhi() //异步获取全局金币值
      this.createRewardedVideoAd() // 在页面onLoad回调事件中创建激励视频广告实例
    },
    // 观看
    look(e) {
      // 用户触发广告后，显示激励视频广告
      if (videoAd) {
        videoAd.show().catch(() => {
          // 失败重试
          videoAd.load()
            .then(() => videoAd.show())
            .catch(err => {
              wx.showToast({
                icon:'none',
                title: '广告未就绪，稍后再来',
              })
            })
        })
      }else{
        this.setData({
          isDisabled:true
        })
        wx.showToast({
          icon:'none',
          title: '广告未就绪，稍后再来',
        })
        setTimeout(()=>{
          this.setData({
            isDisabled:false
          })
        },3000)
      }
    },
    // 在页面onLoad回调事件中创建激励视频广告实例
    createRewardedVideoAd() {
      if (wx.createRewardedVideoAd) {
        videoAd = wx.createRewardedVideoAd({
          adUnitId: 'adunit-d9ad10e80e40912a'
        })
        videoAd.onLoad(() => {console.log(' videoAd.onLoad')})
        videoAd.onError(() => {console.log(' videoAd.onError')})
        videoAd.onClose((res) => {
          if (res.isEnded) {
            this.addJinbi()
            this.subLookNum() //  获取观看和剩余次数及时间
            wx.showToast({
              icon: 'none',
              title: `金币+${app.globalData.jinbizhi.look_ads}`,
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '没有奖励',
            })
          }
        })
      }
    },
    onShow: function () {
      this.getLookVideos_qiandao() //  获取观看和签到剩余次数及时间
    },
    // 跳转其他小程序
    yesSend() {
      wx.navigateToMiniProgram({
        appId: 'wxa845a8ade7205ca6',
        path: `/pages/news/news`,
        success(res) {
          // 打开成功
        }
      })
    },
    //  获取观看和签到剩余次数及时间
    getLookVideos_qiandao() {
      wx.cloud.callFunction({
        name: 'lookvideosqiandao',
        data: {
          tag: 'get',
          _updateTime: util.formatTime(new Date()).slice(0, 10)
        }
      }).then(res => {
        lookVideosNum = res.result[0].lookVideosNum
        this.setData({
          lookVideosNum: res.result[0].lookVideosNum
        })
      })
    },

    // 添加金币
    addJinbi() {
      wx.cloud.callFunction({
        name: 'user',
        data: {
          tag: 'addJinbi',
          zhi,
          mubiao
        }
      }).then(res => {})
    },
    // 观看次数减1
    subLookNum() {
      wx.cloud.callFunction({
        name: 'lookvideosqiandao',
        data: {
          tag: 'subLookNum',
        }
      }).then(res => {
        this.setData({
          lookVideosNum: res.result
        })
      })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
  })